package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.iknow.IKnow;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.db.IKnowDataBase;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.FriendAdapter;
import com.iknow.util.Loger;
import com.iknow.view.widget.MyListView;
import com.iknow.xmpp.service.Contact;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.PresenceAdapter;
import com.iknow.xmpp.service.XmppBroadcastReceiver;
import com.iknow.xmpp.service.aidl.IBeemRosterListener.Stub;
import com.iknow.xmpp.service.aidl.IKnowUser;
import com.iknow.xmpp.service.aidl.IRoster;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import com.mobclick.android.MobclickAgent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.util.StringUtils;

public class XmppContactsActivity extends Activity
{
  private static final Intent SERVICE_INTENT = new Intent();
  private static final String TAG = "ContactList";
  private View.OnClickListener JoinButtonClickListener;
  private AbsListView.OnScrollListener ListViewOnScrollListener;
  private AdapterView.OnItemClickListener listItemClickListener;
  private FriendAdapter mAdapter;
  private boolean mBinded;
  private int mFirstVisibleItem;
  private List<Friend> mFriendList;
  protected Button mGetMoreButton;
  private final Handler mHandler;
  private boolean mInitOnce;
  private FrameLayout mIntroduceLayout;
  private Button mJoinButton;
  private MyListView mList;
  protected View mListViewFooter;
  private View mListViewHeader;
  private RelativeLayout mLoadingBar;
  private ProgressDialog mProgressDialog;
  private final XmppBroadcastReceiver mReceiver;
  private IRoster mRoster;
  private final IKnowRosterListener mRosterListener;
  private final ServiceConnection mServConn;
  private int mStep;
  private FriendTask mTask;
  private TaskListener mTaskListener;
  private String mUserID;
  private IXmppFacade mXmppFacade;
  private boolean needShow;
  private int offset;
  private PopupWindow pw;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public XmppContactsActivity()
  {
    this.mServConn = new XmppServiceConnection(this, null);
    this.mReceiver = new XmppBroadcastReceiver();
    this.mRosterListener = new IKnowRosterListener(this);
    this.mHandler = new Handler();
    this.offset = 0;
    this.mStep = 10;
    this.pw = null;
    this.JoinButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        if (!(((IKnow)this.this$0.getApplication()).isAccountConfigured()))
        {
          Intent localIntent = new Intent(this.this$0, LoginActivity.class);
          this.this$0.startActivity(localIntent);
        }
        while (true)
        {
          return;
          XmppContactsActivity.access$0(this.this$0);
        }
      }
    };
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        Friend localFriend = XmppContactsActivity.access$1(this.this$0).getItem(paramInt - 1);
        if (localFriend == null);
        while (true)
        {
          return;
          Intent localIntent = new Intent(this.this$0, XmppChatActivity.class);
          localIntent.putExtra("friend_jid", localFriend.getID());
          localIntent.putExtra("contact", new Contact(localFriend.getID()));
          localIntent.putExtra("name", localFriend.getName());
          this.this$0.startActivity(localIntent);
        }
      }
    };
    this.ListViewOnScrollListener = new AbsListView.OnScrollListener(this)
    {
      public void onScroll(, int paramInt1, int paramInt2, int paramInt3)
      {
        if (paramInt1 != XmppContactsActivity.access$2(this.this$0))
          if ((paramInt1 + paramInt2 >= paramInt3) && (XmppContactsActivity.access$3(this.this$0)))
            XmppContactsActivity.access$4(this.this$0);
        while (true)
        {
          return;
          XmppContactsActivity.access$5(this.this$0, paramInt1);
        }
      }

      public void onScrollStateChanged(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case 2:
        case 1:
        case 0:
        }
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return;
              XmppContactsActivity.access$1(this.this$0).setScrolling(true);
            }
            XmppContactsActivity.access$1(this.this$0).setScrolling(true);
          }
          XmppContactsActivity.access$1(this.this$0).scrollIdle(paramAbsListView);
        }
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "FriendTask";
      }

      public void onCancelled()
      {
        this.this$0.mListViewFooter.setVisibility(8);
        XmppContactsActivity.access$1(this.this$0).clearFriend();
        XmppContactsActivity.access$7(this.this$0).clear();
        XmppContactsActivity.access$1(this.this$0).notifyDataSetChanged();
        if (XmppContactsActivity.access$14(this.this$0) != null)
        {
          XmppContactsActivity.access$14(this.this$0).dismiss();
          XmppContactsActivity.access$15(this.this$0, null);
        }
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        Iterator localIterator;
        if (XmppContactsActivity.access$1(this.this$0) == null);
        while (true)
        {
          return;
          if ((XmppContactsActivity.access$7(this.this$0).size() != 0) || (XmppContactsActivity.access$1(this.this$0).getCount() != 0))
            break;
          XmppContactsActivity.access$8(this.this$0).setVisibility(8);
          XmppContactsActivity.access$9(this.this$0).setVisibility(0);
          XmppContactsActivity.access$10(this.this$0).setText("找到更多好友");
        }
        if (XmppContactsActivity.access$7(this.this$0).size() < XmppContactsActivity.access$11(this.this$0))
        {
          XmppContactsActivity.access$12(this.this$0, false);
          XmppContactsActivity.access$6(this.this$0).setVisibility(8);
          localIterator = XmppContactsActivity.access$7(this.this$0).iterator();
        }
        while (true)
        {
          while (true)
          {
            while (true)
            {
              do
              {
                if (localIterator.hasNext())
                  break label254;
                XmppContactsActivity.access$1(this.this$0).sortFriendList();
                XmppContactsActivity.access$1(this.this$0).notifyDataSetChanged();
                XmppContactsActivity.access$7(this.this$0).clear();
                XmppContactsActivity.access$9(this.this$0).setVisibility(8);
                this.this$0.mListViewFooter.setVisibility(0);
                XmppContactsActivity.access$8(this.this$0).setVisibility(0);
              }
              while ((XmppContactsActivity.access$13(this.this$0) == null) || (!(XmppContactsActivity.access$13(this.this$0).isShowing())));
              XmppContactsActivity.access$13(this.this$0).dismiss();
            }
            XmppContactsActivity.access$12(this.this$0, true);
          }
          label254: Friend localFriend = (Friend)localIterator.next();
          XmppContactsActivity.access$1(this.this$0).addFriend(localFriend);
        }
      }

      public void onPreExecute()
      {
        XmppContactsActivity.access$6(this.this$0).setVisibility(0);
      }
    };
  }

  private void doNeedMore()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      startActivity(new Intent(this, SearchFriendsOptionActivity.class));
    }
  }

  private void setupListViewFooter()
  {
    this.mListViewFooter = getLayoutInflater().inflate(2130903121, null);
    this.mLoadingBar = ((RelativeLayout)this.mListViewFooter.findViewById(2131296408));
    this.mList.addFooterView(this.mListViewFooter);
  }

  private void setupListViewHeader()
  {
    this.mListViewHeader = getLayoutInflater().inflate(2130903122, null);
    this.mGetMoreButton = ((Button)this.mListViewHeader.findViewById(2131296480));
    this.mGetMoreButton.setText(getString(2131361965));
    this.mGetMoreButton.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        XmppContactsActivity.access$0(this.this$0);
      }
    });
    this.mList.addHeaderView(this.mListViewHeader);
  }

  private void startGetFriendInfo()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mList.setVisibility(0);
      this.mTask = new FriendTask(this, null);
      this.mTask.setListener(this.mTaskListener);
      this.mTask.execute(new TaskParams[0]);
    }
  }

  public LinearLayout linearContainer()
  {
    ProgressBar localProgressBar = new ProgressBar(this);
    TextView localTextView = new TextView(this);
    LinearLayout localLinearLayout = new LinearLayout(this);
    localLinearLayout.setOrientation(0);
    localTextView.setText("加载中...");
    localTextView.setGravity(16);
    localProgressBar.setPadding(0, 0, 15, 0);
    localLinearLayout.addView(localProgressBar, new LinearLayout.LayoutParams(-2, -2));
    localLinearLayout.addView(localTextView, new LinearLayout.LayoutParams(-2, -2));
    localLinearLayout.setGravity(17);
    localLinearLayout.setBackgroundColor(2131165193);
    return localLinearLayout;
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    setContentView(2130903120);
    this.mIntroduceLayout = ((FrameLayout)findViewById(2131296459));
    this.mJoinButton = ((Button)findViewById(2131296514));
    this.mJoinButton.setOnClickListener(this.JoinButtonClickListener);
    this.mList = ((MyListView)findViewById(2131296466));
    this.mList.setOnItemClickListener(this.listItemClickListener);
    this.mList.setOnScrollListener(this.ListViewOnScrollListener);
    this.mFriendList = new ArrayList();
    this.mAdapter = new FriendAdapter(this, getLayoutInflater());
    setupListViewFooter();
    setupListViewHeader();
    this.mList.setAdapter(this.mAdapter);
    registerReceiver(this.mReceiver, new IntentFilter("IKnowConnectionClosed"));
  }

  protected void onDestroy()
  {
    super.onDestroy();
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mTask.cancel(true);
    this.mAdapter.clearFriend();
    this.mFriendList = null;
    this.mAdapter = null;
    unregisterReceiver(this.mReceiver);
  }

  protected void onPause()
  {
    try
    {
      if (this.mRoster != null)
      {
        this.mRoster.removeRosterListener(this.mRosterListener);
        this.mRoster = null;
      }
      if (this.mBinded)
      {
        getApplicationContext().unbindService(this.mServConn);
        this.mBinded = false;
      }
      MobclickAgent.onPause(this);
      super.onPause();
      return;
    }
    catch (RemoteException localRemoteException)
    {
      Loger.d("ContactList", "Remote exception", localRemoteException);
    }
  }

  protected void onResume()
  {
    String str;
    if (((IKnow)getApplication()).isAccountConfigured())
    {
      str = IKnow.mSystemConfig.getString("account_username");
      if ((this.mUserID != null) && (!(this.mUserID.equalsIgnoreCase(str))))
      {
        this.mAdapter.clearFriend();
        this.mAdapter.notifyDataSetChanged();
        this.mInitOnce = false;
        this.mUserID = str;
        this.offset = 0;
        this.mBinded = getApplicationContext().bindService(SERVICE_INTENT, this.mServConn, 1);
      }
    }
    while (true)
    {
      while (true)
      {
        MobclickAgent.onResume(this);
        MobclickAgent.setSessionContinueMillis(600000L);
        super.onResume();
        return;
        this.mUserID = str;
      }
      this.mList.setVisibility(8);
      this.mIntroduceLayout.setVisibility(0);
    }
  }

  public boolean onSearchRequested()
  {
    boolean bool;
    if (getParent() instanceof TabActivity)
      bool = getParent().onSearchRequested();
    while (true)
    {
      return bool;
      bool = super.onSearchRequested();
    }
  }

  private class FriendTask extends GenericTask
  {
    protected TaskResult _doInBackground()
    {
      Object localObject3;
      List localList = IKnow.mIKnowDataBase.getMyRosterID(3, XmppContactsActivity.access$23(this.this$0), XmppContactsActivity.access$11(this.this$0));
      Iterator localIterator = localList.iterator();
      if (!(localIterator.hasNext()))
      {
        if (!(localList.isEmpty()))
        {
          XmppContactsActivity localXmppContactsActivity = this.this$0;
          XmppContactsActivity.access$24(localXmppContactsActivity, XmppContactsActivity.access$23(localXmppContactsActivity) + localList.size());
        }
        return TaskResult.OK;
      }
      String str = (String)localIterator.next();
      Object localObject1 = IKnow.mIKnowDataBase.getFriendInfo(str);
      if (localObject1 == null);
      try
      {
        if ((XmppContactsActivity.access$17(this.this$0).createConnection() != null) && (XmppContactsActivity.access$17(this.this$0).createConnection().getIKnowUser() != null))
        {
          Friend localFriend = XmppContactsActivity.access$17(this.this$0).createConnection().getIKnowUser().getUserInfo(StringUtils.parseName(str));
          localObject3 = localFriend;
          if (localObject3 == null);
        }
      }
      catch (RemoteException localRemoteException1)
      {
        Object localObject2;
        try
        {
          localObject1 = new Friend(str, null, null);
          while (true)
          {
            while (true)
              XmppContactsActivity.access$7(this.this$0).add(localObject1);
            IKnow.mIKnowDataBase.addFriend((Friend)localObject3);
            localObject1 = localObject3;
          }
          localRemoteException1 = localRemoteException1;
          localObject2 = localRemoteException1;
          ((RemoteException)localObject2).printStackTrace();
        }
        catch (RemoteException localRemoteException2)
        {
          while (true)
          {
            while (true)
            {
              localObject2 = localRemoteException2;
              localObject1 = localObject3;
            }
            localObject3 = localObject1;
          }
        }
      }
    }
  }

  private class IKnowRosterListener extends IBeemRosterListener.Stub
  {
    private void updateCurrentList(, Contact paramContact)
    {
      XmppContactsActivity.access$25(this.this$0).post(new Runnable(this, paramBoolean, paramContact)
      {
        public void run()
        {
          if (!(this.val$add))
            XmppContactsActivity.access$1(XmppContactsActivity.IKnowRosterListener.access$0(this.this$1)).removeFriend(this.val$contact.getJID());
          while (true)
          {
            XmppContactsActivity.access$1(XmppContactsActivity.IKnowRosterListener.access$0(this.this$1)).sortFriendList();
            XmppContactsActivity.access$1(XmppContactsActivity.IKnowRosterListener.access$0(this.this$1)).notifyDataSetChanged();
            return;
            Friend localFriend = IKnow.mIKnowDataBase.getFriendInfo(this.val$contact.getJID());
            XmppContactsActivity.access$1(XmppContactsActivity.IKnowRosterListener.access$0(this.this$1)).addFriend(localFriend);
          }
        }
      });
    }

    public void onEntriesAdded()
      throws RemoteException
    {
      boolean bool;
      Iterator localIterator = paramList.iterator();
      if (!(localIterator.hasNext()))
        return;
      String str = (String)localIterator.next();
      Contact localContact = XmppContactsActivity.access$19(this.this$0).getContact(str);
      if (IKnow.mIKnowDataBase.geteRosterItemSubscribeMode(localContact.getJID()) == 3)
        bool = true;
      while (true)
      {
        while (true)
          updateCurrentList(bool, localContact);
        bool = false;
      }
    }

    public void onEntriesDeleted()
      throws RemoteException
    {
      Loger.d("ContactList", "onEntries deleted " + paramList);
      Iterator localIterator = paramList.iterator();
      while (true)
      {
        if (!(localIterator.hasNext()))
          return;
        updateCurrentList(false, new Contact((String)localIterator.next()));
      }
    }

    public void onEntriesUpdated()
      throws RemoteException
    {
    }

    public void onPresenceChanged()
      throws RemoteException
    {
      XmppContactsActivity.access$25(this.this$0).post(new Runnable(this)
      {
        public void run()
        {
          XmppContactsActivity.access$1(XmppContactsActivity.IKnowRosterListener.access$0(this.this$1)).sortFriendList();
          XmppContactsActivity.access$1(XmppContactsActivity.IKnowRosterListener.access$0(this.this$1)).notifyDataSetChanged();
        }
      });
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      XmppContactsActivity.access$16(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
      try
      {
        XmppContactsActivity.access$18(this.this$0, XmppContactsActivity.access$17(this.this$0).getRoster());
        if (XmppContactsActivity.access$19(this.this$0) != null)
        {
          XmppContactsActivity.access$19(this.this$0).addRosterListener(XmppContactsActivity.access$20(this.this$0));
          Loger.d("ContactList", "add roster listener");
          XmppContactsActivity.access$1(this.this$0).setXmppRoster(XmppContactsActivity.access$19(this.this$0));
        }
        if (!(XmppContactsActivity.access$21(this.this$0)))
        {
          XmppContactsActivity.access$4(this.this$0);
          XmppContactsActivity.access$22(this.this$0, true);
        }
        else
        {
          XmppContactsActivity.access$1(this.this$0).sortFriendList();
        }
      }
      catch (RemoteException localRemoteException)
      {
        localRemoteException.printStackTrace();
      }
    }

    public void onServiceDisconnected()
    {
      try
      {
        XmppContactsActivity.access$19(this.this$0).removeRosterListener(XmppContactsActivity.access$20(this.this$0));
        XmppContactsActivity.access$16(this.this$0, null);
        XmppContactsActivity.access$18(this.this$0, null);
        return;
      }
      catch (RemoteException localRemoteException)
      {
        localRemoteException.printStackTrace();
      }
    }
  }
}